package com.mine.frame.mylandemo.ui.mqtt

import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.GsonUtils
import com.mine.frame.common.base.BaseActivity
import com.mine.frame.common.empty.EmptyPresenter
import com.mine.frame.common.empty.IEmpty
import com.mine.frame.mylandemo.R
import com.third.device.mg.serialport.utils.toHexString
import kotlinx.android.synthetic.main.atv_mqtt_main.*

/**
 * @Date 2022/5/24
 * Description:
 */
class MQTTAtv : BaseActivity<IEmpty, EmptyPresenter>(), IEmpty {

    override fun initLayoutId(): Int = R.layout.atv_mqtt_main

    override fun initPresenter(): EmptyPresenter = EmptyPresenter(this)

    override fun initLayoutAfter(savedInstanceState: Bundle?) {

        ClickUtils.applyGlobalDebouncing(btnConnect){
            MqttClientUtils.getInstance().connectMQTT(this)
        }

        ClickUtils.applyGlobalDebouncing(btnOn){
            val opcode = 0.toByte()
            val cmd = byteArrayOf(1 ,opcode ,5,0,0,0,0,1,0,0,1,-101,0,0,0,67,3,1,0,0,0,0,0,0)
            Log.e("MQTT1 =>" ,GsonUtils.toJson(cmd))
            MqttClientUtils.getInstance().publish(cmd)
        }

        ClickUtils.applyGlobalDebouncing(btnOn2){
            val opcode = 0.toByte()
            val cmd = byteArrayOf(1 ,opcode ,5,0,0,0,0,1,0,0,1,-101,0,0,0,67,3,1,0,0,0,0,0,0)
            Log.e("MQTT2 =>" ,GsonUtils.toJson(cmd))
            MqttClientUtils.getInstance().publish(cmd)
        }

        ClickUtils.applyGlobalDebouncing(btnOff){
            val opcode = 0.toByte()
            val cmd = byteArrayOf(1 ,opcode ,5,0,0,0,0,1,0,0,1,-101,0,0,0,67,3,0,0,0,0,0,0,0)
            MqttClientUtils.getInstance().publish(cmd)
        }

        ClickUtils.applyGlobalDebouncing(btnPingNum){
            val opcode = 2.toByte()
            val cmd = byteArrayOf(1 ,opcode)
            MqttClientUtils.getInstance().publish(cmd)
        }

        ClickUtils.applyGlobalDebouncing(btnPingAll){
            val opcode = 0.toByte()
            val cmd = byteArrayOf(1 ,opcode , -1,-1,-1,-1,0,1,0,0,1,-101,30,0,0,2,0)
            MqttClientUtils.getInstance().publish(cmd)
            Log.e("btnPingAll =>" ,cmd.toHexString())
        }

        ClickUtils.applyGlobalDebouncing(btnPingAll2){
            val opcode = 0.toByte()
            val cmd = byteArrayOf(1 ,opcode , 255.toByte(), 255.toByte(), 255.toByte(), 255.toByte(),0,1,0,0,1, 155.toByte(),10,0,0,2,0)
            MqttClientUtils.getInstance().publish(cmd)
            Log.e("btnPingAll2 =>" ,cmd.toHexString())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        MqttClientUtils.getInstance().disconnect()
    }
}